package com.gxuwz.prrs.activity;

import android.os.Bundle;
import android.view.View;
import android.widget.*;
import androidx.appcompat.app.AppCompatActivity;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.adapter.PianoRoomAdapter;
import com.gxuwz.prrs.bean.PianoRoom;
import com.gxuwz.prrs.bean.PianoRoomQuery;
import com.gxuwz.prrs.service.impl.PianoRoomServiceImpl;
import com.gxuwz.prrs.utils.NavigationUtils;
import com.gxuwz.prrs.utils.PageUtils;

import java.util.LinkedList;
import java.util.List;

/**
 * @author 张钧
 * 琴房查询页
 */
public class PianoRoomsActivity extends AppCompatActivity {
	List<Button> buttonList = new LinkedList<>();

	PianoRoomAdapter pianoRoomAdapter;
	ImageView left;
	ImageView right;

	long current = 1;
	long size = 11;
	String openTime = "星期一";
	String pianoRoomNo;

	private final String[] week = new String[]{"星期一", "星期二", "星期三", "星期四", "星期五", "星期六", "星期天"};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_piano_rooms);
		NavigationUtils.setRedirectBottom(this);
		left = findViewById(R.id.left);
		right = findViewById(R.id.right);
		Spinner spinner = findViewById(R.id.spinner);
		spinner.setAdapter(new ArrayAdapter<>(this, android.R.layout.simple_spinner_item, week));
		NavigationUtils.setBack(R.id.back,this);
		Button search = findViewById(R.id.search);
		EditText pianoRoomNo = findViewById(R.id.search_piano_room_no);
		search.setOnClickListener(view -> {
			this.pianoRoomNo = String.valueOf(pianoRoomNo.getText());
			pianoRoomAdapter.updateData(loadPage(current));
		});
		spinner.setOnItemSelectedListener(this.onItemSelected());
		PageUtils.setupPageButtons(this,buttonList,this::onClick);
		List<PianoRoom> pianoRooms = loadPage(1);
		if(pianoRooms != null){
			pianoRoomAdapter = new PianoRoomAdapter(this,pianoRooms);
			ListView listView = findViewById(R.id.piano_room_list_view);
			listView.setAdapter(pianoRoomAdapter);
		}
		PianoRoomQuery<PianoRoom> page = new PianoRoomQuery<PianoRoom>().setOpenTime(openTime);
		page.setSize(size);
		PageUtils.setupNavigationButtons(this,buttonList,left,right,current,pianoRoomAdapter,PianoRoomServiceImpl.class, page);
	}

	private void onClick(View view) {
		Button button = (Button) view;
		long aim = Long.parseLong(String.valueOf(button.getText()));
		pianoRoomAdapter.updateData(loadPage(aim));
	}

	private List<PianoRoom> loadPage(long current) {
		PianoRoomQuery<PianoRoom> page = new PianoRoomQuery<PianoRoom>().setPianoRoomNo(pianoRoomNo).setOpenTime(openTime);
		page.setCurrent(current).setSize(size);
		List<PianoRoom> pianoRooms = PageUtils.loadPage(this, current, pianoRoomAdapter, PianoRoomServiceImpl.class, page, buttonList, left, right);
		this.current = current;
		return pianoRooms;
	}

	private AdapterView.OnItemSelectedListener onItemSelected(){
		return new AdapterView.OnItemSelectedListener() {
			@Override
			public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
				openTime = week[i];
			}
			@Override
			public void onNothingSelected(AdapterView<?> adapterView) {}
		};
	}
}
